package cn.cdeden.knowledge.service.impl;

import cn.cdeden.common.core.utils.MapstructUtils;
import cn.cdeden.knowledge.domain.PersonalFilesTree;
import cn.cdeden.knowledge.domain.TreeDownloadFiles;
import cn.cdeden.knowledge.domain.dto.FileTemplateTreeDto;
import cn.cdeden.knowledge.domain.dto.QaFilesTreeDto;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.google.common.base.Joiner;
import lombok.RequiredArgsConstructor;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.cdeden.knowledge.domain.QaFilesTree;
import cn.cdeden.knowledge.mapper.QaFilesTreeMapper;
import cn.cdeden.knowledge.service.QaFilesTreeService;
@Service
@RequiredArgsConstructor
public class QaFilesTreeServiceImpl extends ServiceImpl<QaFilesTreeMapper, QaFilesTree> implements QaFilesTreeService{

    private final QaFilesTreeMapper mapper;

    @Override
    public QaFilesTreeDto findById(Long id) {
        return MapstructUtils.convert(getById(id), QaFilesTreeDto.class);
    }

    @Override
    public QaFilesTree selectByParentId(String parentId) {
        return mapper.selectOne(new LambdaQueryWrapper<QaFilesTree>().eq(QaFilesTree::getId, Long.valueOf(parentId)));
    }

    @Override
    public boolean insertOne(QaFilesTree qaFilesTree) {
        return mapper.insertOrUpdate(qaFilesTree);
    }

    @Override
    public List<TreeDownloadFiles> folderSonFiles(List<String> ids, Boolean b) {
        if (CollectionUtils.isEmpty(ids) || ids.size() <= 0) {
            return new ArrayList<>();
        }
        List<QaFilesTree> trees1 = selectFilesTree(ids);
        List<TreeDownloadFiles> filesList = MapstructUtils.convert(trees1, TreeDownloadFiles.class);

        String join = Joiner.on(",").join(ids);
        List<QaFilesTree> trees = mapper.selectAllSubordinate(join,trees1.get(0).getCreateId(), b);

        List<TreeDownloadFiles> filesTrees = MapstructUtils.convert(trees, TreeDownloadFiles.class);
        //不要树结构
        boolean b1 = filesTrees.addAll(filesList);
        return filesTrees;
    }


    @Override
    public List<QaFilesTree> selectFilesTree(List<String> ids) {
        QueryWrapper<QaFilesTree> predicate = new QueryWrapper<>();
        predicate.in("id", ids)
//                .eq("type",TreeTypeEnum.FILE.getType())
            .isNull("delete_id")
        ;

        return this.list(predicate);
    }

    @Override
    public QaFilesTree getStorageId(String fileName, String userId) {
        return mapper.selectOne(new LambdaQueryWrapper<QaFilesTree>()
            .eq(QaFilesTree::getRealName, fileName)
            .eq(QaFilesTree::getCreateId, userId).orderByDesc(QaFilesTree::getCreateTime).last("LIMIT 1"));
    }


}
